﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NLUNZ.UserControls
{
    public partial class BulkUpdateCustom : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            int DelivRecord, err, cor = 0;
            bool isCor = true;
            string st = "";
            string log;

            string opnFileName = fuExcel.FileName;


            bool isExcel = Utility.IsExcel(opnFileName);

            if (isExcel == true)
            {
                string path = Server.MapPath("~/Upload/Custom/");
                string strNewFile = Utility.NewFileName(opnFileName,path);
                fuExcel.SaveAs(strNewFile);

                DataSet ds = Utility.ExcelToDataSet(strNewFile);
                if (ds.Tables.Count == 0)
                {
                    Literal1.Text = "错误,你所上传的文件没有记录.";
                }
                else
                {
                    DataTable dt = ds.Tables[0];
                    Delivery delivery = new Delivery();

                    try
                    {
                        gvTest.DataSource = dt.DefaultView;
                        gvTest.DataBind();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if(!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim())){
                            DelivRecord = i + 2;
                            if (dt.Rows[i].ToString().Trim() != "")
                            {
                                delivery.DeliveryNum = dt.Rows[i][0].ToString();
                                DateTime customDate = DateTime.Parse(dt.Rows[i][1].ToString());
                                delivery.CustomDate = customDate;

                                delivery.IsCheckedOut = true;
                                

                                if(!Repository.UpdateDeliveryRecordCustomInformation(delivery))
                                {
                                    err = DelivRecord;
                                    st = st + " " + err.ToString();
                                    isCor = false;
                                }
                                else
                                {
                                    cor++;
                                }
                            }
                            }
                        }

                        log = DateTime.UtcNow.AddHours(12).ToString("yyyy-MM-dd HH:mm:ss") + " 已经更新 " + cor.ToString() + " 条记录.";

                        if (isCor == true)
                        {
                            Literal1.Text = log;
                        }
                        else
                        {
                            log = log + "第" + st + " 行数据有误,更新失败.";
                            Literal1.Text = log;
                        }
                        //System.IO.File.Delete(strNewFile);
                    }
                    catch
                    {
                        Literal1.Text = "错误,文件数据有误,更新失败.";
                        //System.IO.File.Delete(strNewFile);
                    }
                    //GridView1DataBind();
                }
            }
            else
            {
                Literal1.Text = "错误,只能上传Excel文件.";
            }
        }
    }
}